package com.sixteengroup.personnelms.controller.finance;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixteengroup.personnelms.bean.finance.GongZiManage;
import com.sixteengroup.personnelms.bean.finance.PageTo;
import com.sixteengroup.personnelms.bean.finance.TraProgram;
import com.sixteengroup.personnelms.bean.to.RevSalary;
import com.sixteengroup.personnelms.service.finance.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(path = "/finance")
public class FinanceController {

    @Autowired
    FinanceService financeService;
    /**
     * 管理工资
     * */
    @RequestMapping("GZManages/{pageNum}/{pageSize}")
    public String gzManages(PageTo pageTo) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            PageHelper.startPage(pageTo.getPageNum(), pageTo.getPageSize());
            PageInfo<GongZiManage> articlePageInfo = new PageInfo<>(financeService.queryAllStaff());
            List<GongZiManage> articlesLimitList = articlePageInfo.getList();
            //articlesLimitList.forEach(System.out::println);
            PageInfo<TraProgram> articlePageInfo1 = new PageInfo<>(financeService.mixSel());
            List<TraProgram> articlesLimitList1 = articlePageInfo1.getList();
            map.put("data", articlesLimitList);
            map.put("code", "200");
            map.put("total", articlePageInfo.getTotal());
            map.put("dataa",articlesLimitList1);
        } catch (Exception e) {
            map.put("code", "500");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改工资
     * */
    @PostMapping("revSalary")
    public String revSalary(@RequestBody RevSalary revSalary) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            map.put("id",revSalary.getId());
            map.put("base",revSalary.getBase());
            map.put("performance",revSalary.getBonus());
            financeService.revSalary(map);
            map.put("code", "200");
        } catch (Exception e) {
            map.put("code", "500");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 培训统计
     * */
    @RequestMapping("addTrain/{pageNum}/{pageSize}")
    public String addTrain(PageTo pageTo) {
        System.out.println("****************");
        HashMap<String, Object> map = new HashMap<>();
        try {
            PageHelper.startPage(pageTo.getPageNum(), pageTo.getPageSize());
            PageInfo<TraProgram> articlePageInfo = new PageInfo<>(financeService.queryAllPro());
            List<TraProgram> articlesLimitList = articlePageInfo.getList();
            PageInfo<TraProgram> articlePageInfo1 = new PageInfo<>(financeService.mixSel());
            List<TraProgram> articlesLimitList1 = articlePageInfo1.getList();
            map.put("data", articlesLimitList);
            map.put("code", "200");
            map.put("total", articlePageInfo.getTotal());
            map.put("dataa",articlesLimitList1);
            System.out.println(articlesLimitList);
            System.out.println(articlesLimitList1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "500");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 按名称培训统计
     * */
    @RequestMapping("mixSel/{pageNum}/{pageSize}/{id}")
    public String mixSel(PageTo pageTo) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            PageHelper.startPage(pageTo.getPageNum(), pageTo.getPageSize());
            PageInfo<TraProgram> articlePageInfo = new PageInfo<>(financeService.queryProByName(pageTo.getId()));
            List<TraProgram> articlesLimitList = articlePageInfo.getList();
            PageInfo<TraProgram> articlePageInfo1 = new PageInfo<>(financeService.mixSel());
            System.out.println("----------------------------------------");
            System.out.println(articlePageInfo1);
            List<TraProgram> articlesLimitList1 = articlePageInfo1.getList();
            System.out.println(articlesLimitList1);
            map.put("data", articlesLimitList);
            map.put("code", "200");
            map.put("total", articlePageInfo.getTotal());
            map.put("dataa",articlesLimitList1);
            //map.put("data2",articlesLimitList1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", "500");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 插入一条新活动
     * */
    @RequestMapping("insertBase")
    public String insertBase(@RequestBody RevSalary revSalary) {
        System.out.println(revSalary);
        HashMap<String, Object> map = new HashMap<>();

            map.put("id",revSalary.getId());
            map.put("base1",revSalary.getBase1());
            map.put("date",revSalary.getDate());
            map.put("bonus",revSalary.getBonus());
            map.put("stid",revSalary.getId());
            System.out.println("oldID"+revSalary.getId());
            financeService.insertBase(map);
            int i = financeService.selLimit();
            map.put("traid",i);
            financeService.insertBasePeo(map);

            map.put("code", "200");
           // map.put("total", articlePageInfo.getTotal());
           // map.put("dataa",articlesLimitList1);
            //map.put("data2",articlesLimitList1);
          return JSON.toJSONString(map);
    }

    @RequestMapping("delOne/{id}")
    public String delOne(@PathVariable String id) {
        System.out.println("idddd==="+id);
        HashMap<String, Object> map = new HashMap<>();
        try{
        financeService.delOne(Integer.valueOf(id));
        financeService.delTwo(Integer.valueOf(id));
        map.put("code", "200");
        }catch (Exception e){
            map.put("code", "500");
        }
        return JSON.toJSONString(map);
    }
    /**
     * 员工有哪些需要参加项目
     * */
    @RequestMapping("selAttend/{pageNum}/{pageSize}/{id}")
    public String selAttend(PageTo pageTo) {
        System.out.println("id======"+pageTo.getId());
        System.out.println(pageTo);
        System.out.println(financeService.selAttend(pageTo.getId()));
        HashMap<String, Object> map = new HashMap<>();
        try {
            PageHelper.startPage(pageTo.getPageNum(), pageTo.getPageSize());
            PageInfo<TraProgram> articlePageInfo = new PageInfo<>(financeService.selAttend(pageTo.getId()));
            List<TraProgram> articlesLimitList = articlePageInfo.getList();
            map.put("data", articlesLimitList);
            map.put("code", "200");
            map.put("total", articlePageInfo.getTotal());
        } catch (Exception e) {
            map.put("code", "500");
        }
        return JSON.toJSONString(map);
    }
    /**
     * 员工确认参加项目
     * */
    @RequestMapping("updateState/{id}")
    public String updateState(@PathVariable String id) {
        System.out.println("idddd==="+id);
        HashMap<String, Object> map = new HashMap<>();
        try{
            financeService.updateState(Integer.valueOf(id));
            map.put("code", "200");
        }catch (Exception e){
            map.put("code", "500");
        }
        return JSON.toJSONString(map);
    }
}
